package com.store.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.CommissionRecordAdapter;
import com.store.app.bean.CommissionRecordBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 佣金-待入账佣金 页面。
 * Created by Liam on 2018/3/30.
 */

public class Commission2bAccountedActivity extends BaseActivity
        implements IOnHttpListener, View.OnClickListener {

    private static final int REQ_COMMISSION_2B_ACCOUNTED_REFRESH = 1;
    private static final int REQ_COMMISSION_2B_ACCOUNTED_LOAD_MORE = 2;

    private TextView tvToBeAccounted;
    private AutoListView autoListView;
    // title bar.
    private ImageView ivReturn;
    private TextView title;
    private TextView right;

    private NetRequest netRequest;
    private double toBeAccounted;
    private List<CommissionRecordBean> list = new ArrayList<>();
    private CommissionRecordAdapter adapter;
    private int pageNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commission_2b_accounted);
        ExitApplication.getInstance().addActivity(this);
        pageNo = 1;
        init();
        initView();
        intiClicks();
        initData();
    }

    private void initData() {
        initListView();

        // get data from intent
        Intent intent = getIntent();
        toBeAccounted = intent.getDoubleExtra("2b_accounted", 0.00);
        String text = MoneyUtil.addComma(MoneyUtil.formatMoney(String.valueOf(toBeAccounted)));
        tvToBeAccounted.setText(text);
    }

    private void initListView() {
        if (list == null) list = new ArrayList<>();

        adapter = new CommissionRecordAdapter(this, list);
        autoListView.setAdapter(adapter);
        autoListView.setOnRefreshListener(new AutoListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                netRequest.requestToBeAccountedCommissionList(
                        REQ_COMMISSION_2B_ACCOUNTED_REFRESH, pageNo);
                autoListView.onRefreshComplete();
            }
        });
        autoListView.setOnLoadListener(new AutoListView.OnLoadListener() {
            @Override
            public void onLoad() {
                netRequest.requestToBeAccountedCommissionList(
                        REQ_COMMISSION_2B_ACCOUNTED_LOAD_MORE, pageNo);
                autoListView.onLoadComplete();
            }
        });
        // http request.
        autoListView.onRefresh();
    }

    private void intiClicks() {
        // title bar.
        title.setText("待入账收益明细");
        ivReturn.setOnClickListener(this);
        right.setBackgroundResource(R.drawable.super_return_help);
        right.setOnClickListener(this);
    }

    private void initView() {
        ivReturn = (ImageView) findViewById(R.id.public_ll_return);
        title = (TextView) findViewById(R.id.tvTitle);
        right = (TextView) findViewById(R.id.public_tv_right);

        tvToBeAccounted = (TextView) findViewById(R.id.tv_commission_2b_account_value);
        autoListView = (AutoListView) findViewById(R.id.lv_commission_2b_accounted_list);
    }

    private void init() {
        netRequest = new NetRequest(this);
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        if (requestId == REQ_COMMISSION_2B_ACCOUNTED_REFRESH) { // refresh
            try {
                JSONObject data = new JSONObject(result);
                JSONArray jsonArray = data.optJSONArray("list");
                Gson gson = new Gson();
                List<CommissionRecordBean> list = new ArrayList<>();
                CommissionRecordBean temp;
                for (int i = 0; i < jsonArray.length(); i++) {
                    temp = gson.fromJson(jsonArray.optJSONObject(i).toString(), CommissionRecordBean.class);
                    list.add(temp);
                }
                autoListView.onRefreshComplete();
                if (data.optJSONObject("page").optInt("page_no") == pageNo) {
                    // 设置footer的展示效果 根据没从从服务器获取的数据的条数判断是否加载完毕
                    autoListView.setResultSize(list.size());
                    this.list.clear();
                    this.list.addAll(list);
                    adapter.notifyDataSetChanged();
                    pageNo += 1;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else if (requestId == REQ_COMMISSION_2B_ACCOUNTED_LOAD_MORE) { // load more
            try {
                JSONObject data = new JSONObject(result);
                JSONArray jsonArray = data.optJSONArray("list");
                Gson gson = new Gson();
                List<CommissionRecordBean> list = new ArrayList<>();
                CommissionRecordBean temp;
                for (int i = 0; i < jsonArray.length(); i++) {
                    temp = gson.fromJson(jsonArray.optJSONObject(i).toString(), CommissionRecordBean.class);
                    list.add(temp);
                }
                autoListView.onLoadComplete();
                if (data.optJSONObject("page").optInt("page_no") == pageNo) {
                    autoListView.setResultSize(list.size());
                    this.list.addAll(list);
                    adapter.notifyDataSetChanged();
                    pageNo += 1;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.public_tv_right:
                Intent intent = new Intent(Commission2bAccountedActivity.this, WebViewMoreActivity.class);
                intent.putExtra("url", "http://help.meitianhui.com/?id=1009");
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    public static void start(Activity activity, double toBeAccounted) {
        Intent intent = new Intent(activity, Commission2bAccountedActivity.class);
        intent.putExtra("2b_accounted", toBeAccounted);
        activity.startActivity(intent);
    }

}
